{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Year</th>\n",
       "      <th>State.Code</th>\n",
       "      <th>Total.Math</th>\n",
       "      <th>Family Income.Between 20-40k.Math</th>\n",
       "      <th>Family Income.Between 40-60k.Math</th>\n",
       "      <th>Family Income.Between 60-80k.Math</th>\n",
       "      <th>Family Income.Between 80-100k.Math</th>\n",
       "      <th>Family Income.Less than 20k.Math</th>\n",
       "      <th>Family Income.More than 100k.Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2005</td>\n",
       "      <td>AL</td>\n",
       "      <td>559</td>\n",
       "      <td>513</td>\n",
       "      <td>539</td>\n",
       "      <td>550</td>\n",
       "      <td>566</td>\n",
       "      <td>462</td>\n",
       "      <td>588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2005</td>\n",
       "      <td>AK</td>\n",
       "      <td>519</td>\n",
       "      <td>492</td>\n",
       "      <td>517</td>\n",
       "      <td>513</td>\n",
       "      <td>528</td>\n",
       "      <td>464</td>\n",
       "      <td>541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2005</td>\n",
       "      <td>AZ</td>\n",
       "      <td>530</td>\n",
       "      <td>498</td>\n",
       "      <td>520</td>\n",
       "      <td>524</td>\n",
       "      <td>534</td>\n",
       "      <td>485</td>\n",
       "      <td>554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2005</td>\n",
       "      <td>AR</td>\n",
       "      <td>552</td>\n",
       "      <td>513</td>\n",
       "      <td>543</td>\n",
       "      <td>553</td>\n",
       "      <td>570</td>\n",
       "      <td>489</td>\n",
       "      <td>572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2005</td>\n",
       "      <td>CA</td>\n",
       "      <td>522</td>\n",
       "      <td>477</td>\n",
       "      <td>506</td>\n",
       "      <td>521</td>\n",
       "      <td>535</td>\n",
       "      <td>451</td>\n",
       "      <td>566</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Year State.Code  Total.Math  Family Income.Between 20-40k.Math  \\\n",
       "0  2005         AL         559                                513   \n",
       "1  2005         AK         519                                492   \n",
       "2  2005         AZ         530                                498   \n",
       "3  2005         AR         552                                513   \n",
       "4  2005         CA         522                                477   \n",
       "\n",
       "   Family Income.Between 40-60k.Math  Family Income.Between 60-80k.Math  \\\n",
       "0                                539                                550   \n",
       "1                                517                                513   \n",
       "2                                520                                524   \n",
       "3                                543                                553   \n",
       "4                                506                                521   \n",
       "\n",
       "   Family Income.Between 80-100k.Math  Family Income.Less than 20k.Math  \\\n",
       "0                                 566                               462   \n",
       "1                                 528                               464   \n",
       "2                                 534                               485   \n",
       "3                                 570                               489   \n",
       "4                                 535                               451   \n",
       "\n",
       "   Family Income.More than 100k.Math  \n",
       "0                                588  \n",
       "1                                541  \n",
       "2                                554  \n",
       "3                                572  \n",
       "4                                566  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = '../data/sat-scores.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['Year', 'State.Code', 'Total.Math', \n",
    "                         'Family Income.Less than 20k.Math', \n",
    "                         'Family Income.Between 20-40k.Math', \n",
    "                         'Family Income.Between 40-60k.Math', \n",
    "                         'Family Income.Between 60-80k.Math',\n",
    "                         'Family Income.Between 80-100k.Math',\n",
    "                         'Family Income.More than 100k.Math'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Year</th>\n",
       "      <th>State.Code</th>\n",
       "      <th>Total.Math</th>\n",
       "      <th>20k&lt;income&lt;40k</th>\n",
       "      <th>40k&lt;income&lt;60k</th>\n",
       "      <th>60k&lt;income&lt;80k</th>\n",
       "      <th>80k&lt;income&lt;100k</th>\n",
       "      <th>income&lt;20k</th>\n",
       "      <th>income&gt;100k</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2005</td>\n",
       "      <td>AL</td>\n",
       "      <td>559</td>\n",
       "      <td>513</td>\n",
       "      <td>539</td>\n",
       "      <td>550</td>\n",
       "      <td>566</td>\n",
       "      <td>462</td>\n",
       "      <td>588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2005</td>\n",
       "      <td>AK</td>\n",
       "      <td>519</td>\n",
       "      <td>492</td>\n",
       "      <td>517</td>\n",
       "      <td>513</td>\n",
       "      <td>528</td>\n",
       "      <td>464</td>\n",
       "      <td>541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2005</td>\n",
       "      <td>AZ</td>\n",
       "      <td>530</td>\n",
       "      <td>498</td>\n",
       "      <td>520</td>\n",
       "      <td>524</td>\n",
       "      <td>534</td>\n",
       "      <td>485</td>\n",
       "      <td>554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2005</td>\n",
       "      <td>AR</td>\n",
       "      <td>552</td>\n",
       "      <td>513</td>\n",
       "      <td>543</td>\n",
       "      <td>553</td>\n",
       "      <td>570</td>\n",
       "      <td>489</td>\n",
       "      <td>572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2005</td>\n",
       "      <td>CA</td>\n",
       "      <td>522</td>\n",
       "      <td>477</td>\n",
       "      <td>506</td>\n",
       "      <td>521</td>\n",
       "      <td>535</td>\n",
       "      <td>451</td>\n",
       "      <td>566</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Year State.Code  Total.Math  20k<income<40k  40k<income<60k  \\\n",
       "0  2005         AL         559             513             539   \n",
       "1  2005         AK         519             492             517   \n",
       "2  2005         AZ         530             498             520   \n",
       "3  2005         AR         552             513             543   \n",
       "4  2005         CA         522             477             506   \n",
       "\n",
       "   60k<income<80k  80k<income<100k  income<20k  income>100k  \n",
       "0             550              566         462          588  \n",
       "1             513              528         464          541  \n",
       "2             524              534         485          554  \n",
       "3             553              570         489          572  \n",
       "4             521              535         451          566  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Rename the income-related column names\n",
    "df = df.rename(columns={'Family Income.Less than 20k.Math':'income<20k',\n",
    "                'Family Income.Between 20-40k.Math':'20k<income<40k',\n",
    "                'Family Income.Between 40-60k.Math':'40k<income<60k',\n",
    "                'Family Income.Between 60-80k.Math':'60k<income<80k',\n",
    "                'Family Income.Between 80-100k.Math':'80k<income<100k',\n",
    "                'Family Income.More than 100k.Math':'income>100k'})\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Total.Math</th>\n",
       "      <th>20k&lt;income&lt;40k</th>\n",
       "      <th>40k&lt;income&lt;60k</th>\n",
       "      <th>60k&lt;income&lt;80k</th>\n",
       "      <th>80k&lt;income&lt;100k</th>\n",
       "      <th>income&lt;20k</th>\n",
       "      <th>income&gt;100k</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>535.653846</td>\n",
       "      <td>488.653846</td>\n",
       "      <td>522.673077</td>\n",
       "      <td>536.076923</td>\n",
       "      <td>548.942308</td>\n",
       "      <td>427.596154</td>\n",
       "      <td>572.173077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>537.480769</td>\n",
       "      <td>502.923077</td>\n",
       "      <td>523.769231</td>\n",
       "      <td>534.903846</td>\n",
       "      <td>550.461538</td>\n",
       "      <td>461.019231</td>\n",
       "      <td>572.519231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>535.339623</td>\n",
       "      <td>494.849057</td>\n",
       "      <td>519.490566</td>\n",
       "      <td>533.188679</td>\n",
       "      <td>545.698113</td>\n",
       "      <td>457.924528</td>\n",
       "      <td>565.169811</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>535.981132</td>\n",
       "      <td>523.622642</td>\n",
       "      <td>547.471698</td>\n",
       "      <td>549.188679</td>\n",
       "      <td>557.641509</td>\n",
       "      <td>478.641509</td>\n",
       "      <td>564.566038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>540.803922</td>\n",
       "      <td>527.823529</td>\n",
       "      <td>550.980392</td>\n",
       "      <td>553.941176</td>\n",
       "      <td>565.333333</td>\n",
       "      <td>482.058824</td>\n",
       "      <td>585.784314</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>540.843137</td>\n",
       "      <td>499.274510</td>\n",
       "      <td>522.000000</td>\n",
       "      <td>534.235294</td>\n",
       "      <td>547.627451</td>\n",
       "      <td>477.039216</td>\n",
       "      <td>569.274510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011</th>\n",
       "      <td>533.226415</td>\n",
       "      <td>494.886792</td>\n",
       "      <td>513.415094</td>\n",
       "      <td>528.660377</td>\n",
       "      <td>541.849057</td>\n",
       "      <td>460.452830</td>\n",
       "      <td>563.245283</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>533.603774</td>\n",
       "      <td>492.056604</td>\n",
       "      <td>512.452830</td>\n",
       "      <td>525.773585</td>\n",
       "      <td>538.301887</td>\n",
       "      <td>458.773585</td>\n",
       "      <td>557.320755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013</th>\n",
       "      <td>532.622642</td>\n",
       "      <td>490.132075</td>\n",
       "      <td>511.377358</td>\n",
       "      <td>520.320755</td>\n",
       "      <td>537.396226</td>\n",
       "      <td>469.358491</td>\n",
       "      <td>556.339623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <td>534.283019</td>\n",
       "      <td>497.641509</td>\n",
       "      <td>514.943396</td>\n",
       "      <td>527.169811</td>\n",
       "      <td>543.132075</td>\n",
       "      <td>459.415094</td>\n",
       "      <td>555.433962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>533.094340</td>\n",
       "      <td>491.603774</td>\n",
       "      <td>513.754717</td>\n",
       "      <td>527.132075</td>\n",
       "      <td>542.037736</td>\n",
       "      <td>447.490566</td>\n",
       "      <td>563.433962</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Total.Math  20k<income<40k  40k<income<60k  60k<income<80k  \\\n",
       "Year                                                               \n",
       "2005  535.653846      488.653846      522.673077      536.076923   \n",
       "2006  537.480769      502.923077      523.769231      534.903846   \n",
       "2007  535.339623      494.849057      519.490566      533.188679   \n",
       "2008  535.981132      523.622642      547.471698      549.188679   \n",
       "2009  540.803922      527.823529      550.980392      553.941176   \n",
       "2010  540.843137      499.274510      522.000000      534.235294   \n",
       "2011  533.226415      494.886792      513.415094      528.660377   \n",
       "2012  533.603774      492.056604      512.452830      525.773585   \n",
       "2013  532.622642      490.132075      511.377358      520.320755   \n",
       "2014  534.283019      497.641509      514.943396      527.169811   \n",
       "2015  533.094340      491.603774      513.754717      527.132075   \n",
       "\n",
       "      80k<income<100k  income<20k  income>100k  \n",
       "Year                                            \n",
       "2005       548.942308  427.596154   572.173077  \n",
       "2006       550.461538  461.019231   572.519231  \n",
       "2007       545.698113  457.924528   565.169811  \n",
       "2008       557.641509  478.641509   564.566038  \n",
       "2009       565.333333  482.058824   585.784314  \n",
       "2010       547.627451  477.039216   569.274510  \n",
       "2011       541.849057  460.452830   563.245283  \n",
       "2012       538.301887  458.773585   557.320755  \n",
       "2013       537.396226  469.358491   556.339623  \n",
       "2014       543.132075  459.415094   555.433962  \n",
       "2015       542.037736  447.490566   563.433962  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Find the average SAT math score for each income level, grouped and then sorted by year.\n",
    "df.groupby('Year').mean(numeric_only=True).sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Year</th>\n",
       "      <th>2005</th>\n",
       "      <th>2006</th>\n",
       "      <th>2007</th>\n",
       "      <th>2008</th>\n",
       "      <th>2009</th>\n",
       "      <th>2010</th>\n",
       "      <th>2011</th>\n",
       "      <th>2012</th>\n",
       "      <th>2013</th>\n",
       "      <th>2014</th>\n",
       "      <th>2015</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>income&lt;20k</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20k&lt;income&lt;40k</th>\n",
       "      <td>0.142793</td>\n",
       "      <td>0.090894</td>\n",
       "      <td>0.080635</td>\n",
       "      <td>0.093977</td>\n",
       "      <td>0.094936</td>\n",
       "      <td>0.046611</td>\n",
       "      <td>0.074783</td>\n",
       "      <td>0.072548</td>\n",
       "      <td>0.044260</td>\n",
       "      <td>0.083207</td>\n",
       "      <td>0.098579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40k&lt;income&lt;60k</th>\n",
       "      <td>0.069618</td>\n",
       "      <td>0.041450</td>\n",
       "      <td>0.049796</td>\n",
       "      <td>0.045546</td>\n",
       "      <td>0.043872</td>\n",
       "      <td>0.045517</td>\n",
       "      <td>0.037439</td>\n",
       "      <td>0.041451</td>\n",
       "      <td>0.043346</td>\n",
       "      <td>0.034768</td>\n",
       "      <td>0.045059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60k&lt;income&lt;80k</th>\n",
       "      <td>0.025645</td>\n",
       "      <td>0.021259</td>\n",
       "      <td>0.026368</td>\n",
       "      <td>0.003136</td>\n",
       "      <td>0.005374</td>\n",
       "      <td>0.023439</td>\n",
       "      <td>0.029694</td>\n",
       "      <td>0.025994</td>\n",
       "      <td>0.017489</td>\n",
       "      <td>0.023743</td>\n",
       "      <td>0.026038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80k&lt;income&lt;100k</th>\n",
       "      <td>0.023999</td>\n",
       "      <td>0.029085</td>\n",
       "      <td>0.023462</td>\n",
       "      <td>0.015391</td>\n",
       "      <td>0.020566</td>\n",
       "      <td>0.025068</td>\n",
       "      <td>0.024947</td>\n",
       "      <td>0.023828</td>\n",
       "      <td>0.032817</td>\n",
       "      <td>0.030279</td>\n",
       "      <td>0.028277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>income&gt;100k</th>\n",
       "      <td>0.042319</td>\n",
       "      <td>0.040071</td>\n",
       "      <td>0.035682</td>\n",
       "      <td>0.012418</td>\n",
       "      <td>0.036175</td>\n",
       "      <td>0.039529</td>\n",
       "      <td>0.039487</td>\n",
       "      <td>0.035331</td>\n",
       "      <td>0.035250</td>\n",
       "      <td>0.022650</td>\n",
       "      <td>0.039474</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Year                 2005      2006      2007      2008      2009      2010  \\\n",
       "income<20k            NaN       NaN       NaN       NaN       NaN       NaN   \n",
       "20k<income<40k   0.142793  0.090894  0.080635  0.093977  0.094936  0.046611   \n",
       "40k<income<60k   0.069618  0.041450  0.049796  0.045546  0.043872  0.045517   \n",
       "60k<income<80k   0.025645  0.021259  0.026368  0.003136  0.005374  0.023439   \n",
       "80k<income<100k  0.023999  0.029085  0.023462  0.015391  0.020566  0.025068   \n",
       "income>100k      0.042319  0.040071  0.035682  0.012418  0.036175  0.039529   \n",
       "\n",
       "Year                 2011      2012      2013      2014      2015  \n",
       "income<20k            NaN       NaN       NaN       NaN       NaN  \n",
       "20k<income<40k   0.074783  0.072548  0.044260  0.083207  0.098579  \n",
       "40k<income<60k   0.037439  0.041451  0.043346  0.034768  0.045059  \n",
       "60k<income<80k   0.029694  0.025994  0.017489  0.023743  0.026038  \n",
       "80k<income<100k  0.024947  0.023828  0.032817  0.030279  0.028277  \n",
       "income>100k      0.039487  0.035331  0.035250  0.022650  0.039474  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# For each year in our data set, find out much better each income group did,\n",
    "# on average, than the next-poorer group of students. Do we see (just by looking \n",
    "# at the data) any income group that did worse, in any year, \n",
    "# than the next-poorer students?\n",
    "\n",
    "(\n",
    "    df\n",
    "    .groupby('Year')\n",
    "    [['income<20k',\n",
    "      '20k<income<40k',\n",
    "      '40k<income<60k',\n",
    "      '60k<income<80k',\n",
    "      '80k<income<100k',\n",
    "      'income>100k']]\n",
    "    .mean()\n",
    "    .T\n",
    "    .pct_change()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20k<income<40k     0.083929\n",
       "40k<income<60k     0.045260\n",
       "income>100k        0.034399\n",
       "80k<income<100k    0.025247\n",
       "60k<income<80k     0.020744\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Which income bracket, on average, had the greatest advantage over the next-poorer income bracket?\n",
    "\n",
    "(\n",
    "    df\n",
    "    .groupby('Year')\n",
    "    [['income<20k',\n",
    "      '20k<income<40k',\n",
    "      '40k<income<60k',\n",
    "      '60k<income<80k',\n",
    "      '80k<income<100k',\n",
    "      'income>100k']]\n",
    "    .mean()\n",
    "    .T\n",
    "    .pct_change()\n",
    "    .T\n",
    "    .mean()\n",
    "    .sort_values(ascending=False)\n",
    "    .head()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>income&lt;20k</th>\n",
       "      <th>20k&lt;income&lt;40k</th>\n",
       "      <th>40k&lt;income&lt;60k</th>\n",
       "      <th>60k&lt;income&lt;80k</th>\n",
       "      <th>80k&lt;income&lt;100k</th>\n",
       "      <th>income&gt;100k</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [income<20k, 20k<income<40k, 40k<income<60k, 60k<income<80k, 80k<income<100k, income>100k]\n",
       "Index: []"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Can we find, in a calculated and automated way, which income levels\n",
    "# consistently (i.e., across all years) did worse than the next-poorest group?\n",
    "\n",
    "change = (\n",
    "    df\n",
    "    .groupby('Year')\n",
    "    [['income<20k',\n",
    "      '20k<income<40k',\n",
    "      '40k<income<60k',\n",
    "      '60k<income<80k',\n",
    "      '80k<income<100k',\n",
    "      'income>100k']]\n",
    "    .mean()\n",
    "    .pct_change(axis='columns')\n",
    ") \n",
    "\n",
    "change[change < 0].dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
